﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ETRoboConResultEditorExcel
{
    public partial class Sheet6
    {
        private void Sheet6_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet6_Shutdown(object sender, System.EventArgs e)
        {
        }

        private static readonly int POS_YearCol = 2;
        private static readonly int POS_YearRow = 1;
        private static readonly int POS_NameCol = 2;
        private static readonly int POS_NameRow = 2;
        private static readonly int POS_MemoCol = 2;
        private static readonly int POS_MemoRow = 3;
        private static readonly int POS_IdCol = 2;
        private static readonly int POS_IdRow = 4;

        public ContestInfo GetContestInfo()
        {
            int year=-1;
            string name="";
            string memo="";
            string id="";
            if (((Excel.Range)Globals.Sheet6.Cells[POS_YearRow, POS_YearCol]).Value2 != null)
            {
                string yearStr = ((Excel.Range)Globals.Sheet6.Cells[POS_YearRow, POS_YearCol]).Value2.ToString();
                if (!Int32.TryParse(yearStr, out year))
                {
                    return null;
                }
            }
            if (((Excel.Range)Globals.Sheet6.Cells[POS_NameRow, POS_NameCol]).Value2 != null)
            {
                name = ((Excel.Range)Globals.Sheet6.Cells[POS_NameRow, POS_NameCol]).Value2.ToString();
            }
            if (((Excel.Range)Globals.Sheet6.Cells[POS_MemoRow, POS_MemoCol]).Value2 != null)
            {
                memo = ((Excel.Range)Globals.Sheet6.Cells[POS_MemoRow, POS_MemoCol]).Value2.ToString();
            }
            if (((Excel.Range)Globals.Sheet6.Cells[POS_IdRow, POS_IdCol]).Value2 != null)
            {
                id = ((Excel.Range)Globals.Sheet6.Cells[POS_IdRow, POS_IdCol]).Value2.ToString();
            }

            return new ContestInfo { ID = id, Name = name, Memo = memo, Year = year };
        }

        public void SetContestInfo(ContestInfo info)
        {
            if (!String.IsNullOrEmpty(info.ID)){
            ((Excel.Range)Globals.Sheet6.Cells[POS_IdRow, POS_IdCol]).Value2 = info.ID;
            }
            if (!String.IsNullOrEmpty(info.Name))
            {
                ((Excel.Range)Globals.Sheet6.Cells[POS_NameRow, POS_NameCol]).Value2 = info.Name;
            }
            if (!String.IsNullOrEmpty(info.Memo))
            {
                ((Excel.Range)Globals.Sheet6.Cells[POS_MemoRow, POS_MemoCol]).Value2 = info.Memo;
            }
            if (info.Year > 2000)
            {
                ((Excel.Range)Globals.Sheet6.Cells[POS_YearRow, POS_YearCol]).Value2 = info.Year;
            }
        }


        #region VSTO で生成されたコード

        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Sheet6_Startup);
            this.Shutdown += new System.EventHandler(Sheet6_Shutdown);
        }

        #endregion

    }
    public class ContestInfo
    {
        public int Year { get; set; }
        public string Name { get; set; }
        public string Memo { get; set; }
        public string ID { get; set; }
    }
}
